package date0702pm;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import cn.edu.ecut.util.DatabaseType;
import cn.edu.ecut.util.JdbcHelper;

/**
 * 
	create table t_students (
	     id number(5) ,
	     name varchar2(30) ,
	     gender char(6) ,
	     birthdate date ,
	     constraint t_students_id_pk primary key (id)
	);
 */
public class JdbcTest09 {

	public static void main(String[] args) throws SQLException {
		
		JdbcHelper h = JdbcHelper.getInstance( DatabaseType.ORACLE );
		Statement s = h.create() ;
		
		// 因为 id 是主键，是惟一的，因此至多返回一行数据
		String select = "SELECT id , name , gender , birthdate FROM t_students WHERE id = 1 " ;
		// 不论 select 语句是否返回了 数据，结果集对象都会被创建的
		ResultSet rs = s.executeQuery( select );
		System.out.println( "result set : " +  rs );
		
		Student t = null ;
		
		if( rs.next() ) { // 如果 select 语句返回了数据则结果集就会有数据，则说明
			t = new Student(); 
			
			int id = rs.getInt( "id" ); // rs.getInt( 1 );
			t.setId(id);
			
			String name = rs.getString( "name" ) ; // rs.getString( 2 );
			t.setName(name);
			
			String genderString = rs.getString( "gender" ); // rs.getString( 3 );
			if( genderString != null && ( genderString = genderString.trim() ).length() > 0  ) {
				char gender = genderString.charAt( 0 ); // 取字符串中首个字符
				t.setGender(gender);
			}
			
			java.sql.Date date = rs.getDate( "birthdate" );  // rs.getDate( 4 )
			if( date != null ) {
				java.time.LocalDate birthdate = date.toLocalDate();
				t.setBirthdate( birthdate );
			}
		}
		
		h.close( rs );
		h.close( s );
		h.release();
		
		if( t != null ) {
			System.out.println( t.getId() + " ,  " + t.getName() + " , " + t.getGender() + " , " + t.getBirthdate() );
		}

	}

}
